/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import static gui.GUIUtils.reverse;
import static gui.utilities.Verifiers.verifyEmail;
import static gui.utilities.Verifiers.verifyUserName;

import gui.utilities.Rater;

import java.awt.Color;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JTextField;

import core.exceptions.DBAccessException;
import core.objects.Book;
import core.objects.BookLoan;
import core.objects.Country;
import core.objects.User;
import core.objects.enums.GenderEnum;
import db.DBAccessEngine;
import db.IDBAccess;
import db.threads.DBAccessThread;
import db.threads.IDBAccessThread;
import java.util.logging.Level;
import java.util.logging.Logger;

public class JPanelMyProfile extends javax.swing.JPanel {

    private final User user;
    private Rater usersRater;
    private boolean comboDayInited = false;
    private boolean comboMonthInited = false;
    private boolean comboYearInited = false;
    private boolean comboGenderInited = false;
    private boolean comboCountryInited = false;

    /**
     * Creates new form JPanelMyProfile
     */
    public JPanelMyProfile(User user) {
        this.user = user;
        if (user != null) {
            initComponents();
            initHeaderComponents();
            initBooksPanel();
            initLoansPanel();
        }

    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this
     * code. The content of this method is always regenerated by the Form Editor.
     */
    @SuppressWarnings({"unchecked", "rawtypes"})
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanelUserInfoHeaderWithPic = new javax.swing.JPanel();
        jPanelUserHeader = new javax.swing.JPanel();
        jPanelUserTitle = new javax.swing.JPanel();
        jTextFieldUserName = new javax.swing.JTextField();
        jTextFieldEmail = new javax.swing.JTextField();
        jPanelRating = new javax.swing.JPanel();
        jPanelDesc = new javax.swing.JPanel();
        jPanelFirstName = new javax.swing.JPanel();
        jLabelFirstName = new javax.swing.JLabel();
        jTextFieldFirstName = new javax.swing.JTextField();
        jPanelBirthday = new javax.swing.JPanel();
        jLabelBirthday = new javax.swing.JLabel();
        jPanelDate = new javax.swing.JPanel();
        jComboBoxDay = new javax.swing.JComboBox();
        jComboBoxMonth = new javax.swing.JComboBox();
        jComboBoxYear = new javax.swing.JComboBox();
        jPanelGender = new javax.swing.JPanel();
        jLabelGender = new javax.swing.JLabel();
        jPanel = new javax.swing.JPanel();
        jComboBoxGender = new javax.swing.JComboBox();
        jPanelLastName = new javax.swing.JPanel();
        jLabelLastName = new javax.swing.JLabel();
        jTextFieldLastName = new javax.swing.JTextField();
        jPanelCountry = new javax.swing.JPanel();
        jLabelCountry = new javax.swing.JLabel();
        jPanel12 = new javax.swing.JPanel();
        Country countries[];
        try {
            countries = GUIUtils.genCountriesArray();
        } catch (DBAccessException ex) {
            GUIUtils.showMessageError(ex, this);
            countries = null;
        }
        jComboBoxCountry = new javax.swing.JComboBox(countries);
        jPanelSaveOrCancel = new javax.swing.JPanel();
        jLabelCancel = new javax.swing.JLabel();
        jLabelSave = new javax.swing.JLabel();
        jLabelImage = new javax.swing.JLabel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanelMyBooks = new javax.swing.JPanel();
        jScrollPaneBooks = new javax.swing.JScrollPane();
        jPanelBooks = new javax.swing.JPanel();
        jPanelMyLoans = new javax.swing.JPanel();

        setBackground(new java.awt.Color(51, 153, 255));
        addComponentListener(new java.awt.event.ComponentAdapter() {
            @Override
			public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });
        addFocusListener(new java.awt.event.FocusAdapter() {
            @Override
			public void focusGained(java.awt.event.FocusEvent evt) {
                formFocusGained(evt);
            }
        });

        jPanelUserInfoHeaderWithPic.setOpaque(false);

        jPanelUserHeader.setOpaque(false);

        jPanelUserTitle.setOpaque(false);

        jTextFieldUserName.setBackground(new java.awt.Color(51, 153, 255));
        jTextFieldUserName.setText("UserName");
        jTextFieldUserName.setBorder(null);
        jTextFieldUserName.setFocusable(false);
        jTextFieldUserName.setOpaque(false);
        jTextFieldUserName.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
			public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTextFieldUserNameMouseClicked(evt);
            }
        });
        jTextFieldUserName.addFocusListener(new java.awt.event.FocusAdapter() {
            @Override
			public void focusLost(java.awt.event.FocusEvent evt) {
                jTextFieldUserNameFocusLost(evt);
            }
        });

        jTextFieldEmail.setBackground(new java.awt.Color(51, 153, 255));
        jTextFieldEmail.setText("Email");
        jTextFieldEmail.setBorder(null);
        jTextFieldEmail.setFocusable(false);
        jTextFieldEmail.setOpaque(false);
        jTextFieldEmail.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
			public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTextFieldEmailMouseClicked(evt);
            }
        });
        jTextFieldEmail.addFocusListener(new java.awt.event.FocusAdapter() {
            @Override
			public void focusLost(java.awt.event.FocusEvent evt) {
                jTextFieldEmailFocusLost(evt);
            }
        });

        javax.swing.GroupLayout jPanelUserTitleLayout = new javax.swing.GroupLayout(jPanelUserTitle);
        jPanelUserTitle.setLayout(jPanelUserTitleLayout);
        jPanelUserTitleLayout.setHorizontalGroup(
            jPanelUserTitleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelUserTitleLayout.createSequentialGroup()
                .addComponent(jTextFieldUserName, javax.swing.GroupLayout.PREFERRED_SIZE, 208, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jTextFieldEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 226, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanelUserTitleLayout.setVerticalGroup(
            jPanelUserTitleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelUserTitleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jTextFieldUserName, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jTextFieldEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jPanelRating.setOpaque(false);
        jPanelRating.setLayout(new java.awt.GridLayout(1, 1));

        jPanelDesc.setOpaque(false);
        jPanelDesc.setLayout(new java.awt.GridLayout(2, 3));

        jPanelFirstName.setOpaque(false);

        jLabelFirstName.setText("First Name:");

        jTextFieldFirstName.setBackground(new java.awt.Color(51, 153, 255));
        jTextFieldFirstName.setBorder(null);
        jTextFieldFirstName.setFocusable(false);
        jTextFieldFirstName.setOpaque(false);
        jTextFieldFirstName.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
			public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTextFieldFirstNameMouseClicked(evt);
            }
        });
        jTextFieldFirstName.addActionListener(new java.awt.event.ActionListener() {
            @Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldFirstNameActionPerformed(evt);
            }
        });
        jTextFieldFirstName.addFocusListener(new java.awt.event.FocusAdapter() {
            @Override
			public void focusLost(java.awt.event.FocusEvent evt) {
                jTextFieldFirstNameFocusLost(evt);
            }
        });

        javax.swing.GroupLayout jPanelFirstNameLayout = new javax.swing.GroupLayout(jPanelFirstName);
        jPanelFirstName.setLayout(jPanelFirstNameLayout);
        jPanelFirstNameLayout.setHorizontalGroup(
            jPanelFirstNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelFirstNameLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelFirstName, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, 0)
                .addComponent(jTextFieldFirstName, javax.swing.GroupLayout.DEFAULT_SIZE, 199, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanelFirstNameLayout.setVerticalGroup(
            jPanelFirstNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelFirstNameLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelFirstNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabelFirstName, javax.swing.GroupLayout.DEFAULT_SIZE, 20, Short.MAX_VALUE)
                    .addComponent(jTextFieldFirstName))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanelDesc.add(jPanelFirstName);

        jPanelBirthday.setOpaque(false);

        jLabelBirthday.setText("Birthday:");

        jPanelDate.setOpaque(false);
        jPanelDate.setLayout(new java.awt.GridLayout(1, 0));

        jComboBoxDay.setModel(new DefaultComboBoxModel(getIntList(1,31)));
        jComboBoxDay.setBorder(null);
        jComboBoxDay.addItemListener(new java.awt.event.ItemListener() {
            @Override
			public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBoxDayItemStateChanged(evt);
            }
        });
        jPanelDate.add(jComboBoxDay);

        jComboBoxMonth.setModel(new DefaultComboBoxModel(getIntList(1, 12)));
        jComboBoxMonth.setBorder(null);
        jComboBoxMonth.addItemListener(new java.awt.event.ItemListener() {
            @Override
			public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBoxMonthItemStateChanged(evt);
            }
        });
        jPanelDate.add(jComboBoxMonth);

        jComboBoxYear.setModel(new DefaultComboBoxModel(reverse(getIntList(1920, 2014))));
        jComboBoxYear.setBorder(null);
        jComboBoxYear.addItemListener(new java.awt.event.ItemListener() {
            @Override
			public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBoxYearItemStateChanged(evt);
            }
        });
        jPanelDate.add(jComboBoxYear);

        javax.swing.GroupLayout jPanelBirthdayLayout = new javax.swing.GroupLayout(jPanelBirthday);
        jPanelBirthday.setLayout(jPanelBirthdayLayout);
        jPanelBirthdayLayout.setHorizontalGroup(
            jPanelBirthdayLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelBirthdayLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelBirthday, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanelDate, javax.swing.GroupLayout.PREFERRED_SIZE, 215, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanelBirthdayLayout.setVerticalGroup(
            jPanelBirthdayLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelBirthdayLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelBirthdayLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabelBirthday, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanelDate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanelDesc.add(jPanelBirthday);

        jPanelGender.setOpaque(false);

        jLabelGender.setText("Gender:");

        jPanel.setOpaque(false);
        jPanel.setLayout(new java.awt.GridLayout(1, 0));

        jComboBoxGender.setModel(new javax.swing.DefaultComboBoxModel(GenderEnum.values()));
        jComboBoxGender.setBorder(null);
        jComboBoxGender.setOpaque(false);
        jComboBoxGender.addItemListener(new java.awt.event.ItemListener() {
            @Override
			public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBoxGenderItemStateChanged(evt);
            }
        });
        jPanel.add(jComboBoxGender);

        javax.swing.GroupLayout jPanelGenderLayout = new javax.swing.GroupLayout(jPanelGender);
        jPanelGender.setLayout(jPanelGenderLayout);
        jPanelGenderLayout.setHorizontalGroup(
            jPanelGenderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelGenderLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelGender, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanelGenderLayout.setVerticalGroup(
            jPanelGenderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelGenderLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelGenderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabelGender, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanelDesc.add(jPanelGender);

        jPanelLastName.setOpaque(false);

        jLabelLastName.setText("Last Name:");

        jTextFieldLastName.setBackground(new java.awt.Color(51, 153, 255));
        jTextFieldLastName.setBorder(null);
        jTextFieldLastName.setFocusable(false);
        jTextFieldLastName.setOpaque(false);
        jTextFieldLastName.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
			public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTextFieldLastNameMouseClicked(evt);
            }
        });
        jTextFieldLastName.addActionListener(new java.awt.event.ActionListener() {
            @Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldLastNameActionPerformed(evt);
            }
        });
        jTextFieldLastName.addFocusListener(new java.awt.event.FocusAdapter() {
            @Override
			public void focusLost(java.awt.event.FocusEvent evt) {
                jTextFieldLastNameFocusLost(evt);
            }
        });

        javax.swing.GroupLayout jPanelLastNameLayout = new javax.swing.GroupLayout(jPanelLastName);
        jPanelLastName.setLayout(jPanelLastNameLayout);
        jPanelLastNameLayout.setHorizontalGroup(
            jPanelLastNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelLastNameLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelLastName, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, 0)
                .addComponent(jTextFieldLastName, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanelLastNameLayout.setVerticalGroup(
            jPanelLastNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelLastNameLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelLastNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jTextFieldLastName, javax.swing.GroupLayout.DEFAULT_SIZE, 20, Short.MAX_VALUE)
                    .addComponent(jLabelLastName, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanelDesc.add(jPanelLastName);

        jPanelCountry.setOpaque(false);

        jLabelCountry.setText("Country:");

        jPanel12.setOpaque(false);
        jPanel12.setLayout(new java.awt.GridLayout(1, 0));

        jComboBoxCountry.setBorder(null);
        jComboBoxCountry.setOpaque(false);
        jComboBoxCountry.addItemListener(new java.awt.event.ItemListener() {
            @Override
			public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBoxCountryItemStateChanged(evt);
            }
        });
        jPanel12.add(jComboBoxCountry);

        javax.swing.GroupLayout jPanelCountryLayout = new javax.swing.GroupLayout(jPanelCountry);
        jPanelCountry.setLayout(jPanelCountryLayout);
        jPanelCountryLayout.setHorizontalGroup(
            jPanelCountryLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelCountryLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelCountry, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel12, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanelCountryLayout.setVerticalGroup(
            jPanelCountryLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelCountryLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelCountryLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabelCountry, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel12, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanelDesc.add(jPanelCountry);

        jPanelSaveOrCancel.setAlignmentX(1.0F);
        jPanelSaveOrCancel.setOpaque(false);
        jPanelSaveOrCancel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT, 20, 5));

        jLabelCancel.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
			public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabelCancelMouseClicked(evt);
            }
        });
        jPanelSaveOrCancel.add(jLabelCancel);

        jLabelSave.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
			public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabelSaveMouseClicked(evt);
            }
        });
        jPanelSaveOrCancel.add(jLabelSave);

        jPanelDesc.add(jPanelSaveOrCancel);

        javax.swing.GroupLayout jPanelUserHeaderLayout = new javax.swing.GroupLayout(jPanelUserHeader);
        jPanelUserHeader.setLayout(jPanelUserHeaderLayout);
        jPanelUserHeaderLayout.setHorizontalGroup(
            jPanelUserHeaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelUserHeaderLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanelUserTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanelRating, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addComponent(jPanelDesc, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanelUserHeaderLayout.setVerticalGroup(
            jPanelUserHeaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelUserHeaderLayout.createSequentialGroup()
                .addGroup(jPanelUserHeaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelUserHeaderLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanelUserTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanelRating, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanelDesc, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabelImage.setAlignmentY(0.0F);
        jLabelImage.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 51, 51), 1, true));
        jLabelImage.setMaximumSize(new java.awt.Dimension(20, 2));
        jLabelImage.setPreferredSize(new java.awt.Dimension(2, 90));

        javax.swing.GroupLayout jPanelUserInfoHeaderWithPicLayout = new javax.swing.GroupLayout(jPanelUserInfoHeaderWithPic);
        jPanelUserInfoHeaderWithPic.setLayout(jPanelUserInfoHeaderWithPicLayout);
        jPanelUserInfoHeaderWithPicLayout.setHorizontalGroup(
            jPanelUserInfoHeaderWithPicLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelUserInfoHeaderWithPicLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelImage, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanelUserHeader, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(12, 12, 12))
        );
        jPanelUserInfoHeaderWithPicLayout.setVerticalGroup(
            jPanelUserInfoHeaderWithPicLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelUserInfoHeaderWithPicLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelUserInfoHeaderWithPicLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanelUserHeader, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabelImage, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );

        jPanelMyBooks.setOpaque(false);
        jPanelMyBooks.setLayout(new java.awt.BorderLayout());

        jScrollPaneBooks.setBackground(new java.awt.Color(51, 153, 255));

        jPanelBooks.setBackground(new java.awt.Color(51, 153, 255));
        jPanelBooks.addComponentListener(new java.awt.event.ComponentAdapter() {
            @Override
			public void componentShown(java.awt.event.ComponentEvent evt) {
                jPanelBooksComponentShown(evt);
            }
        });
        jPanelBooks.setLayout(new javax.swing.BoxLayout(jPanelBooks, javax.swing.BoxLayout.PAGE_AXIS));
        jScrollPaneBooks.setViewportView(jPanelBooks);

        jPanelMyBooks.add(jScrollPaneBooks, java.awt.BorderLayout.CENTER);

        jTabbedPane1.addTab("My Books", jPanelMyBooks);

        jPanelMyLoans.setLayout(new javax.swing.BoxLayout(jPanelMyLoans, javax.swing.BoxLayout.Y_AXIS));
        jTabbedPane1.addTab("My Loans", jPanelMyLoans);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelUserInfoHeaderWithPic, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanelUserInfoHeaderWithPic, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 332, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jTextFieldFirstNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldFirstNameActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextFieldFirstNameActionPerformed

    private void jTextFieldLastNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldLastNameActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextFieldLastNameActionPerformed

    private void jTextFieldUserNameMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextFieldUserNameMouseClicked
        setJTextFieldEditMode(jTextFieldUserName);
    }//GEN-LAST:event_jTextFieldUserNameMouseClicked

    private void jTextFieldUserNameFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldUserNameFocusLost
        setJTextFieldNonEditMode(jTextFieldUserName);
        if (jTextFieldUserName.getText().equals(user.getName())) {
            return;
        }

        if (!verifyUserName(jTextFieldUserName.getText(), this)) {
            jTextFieldUserName.setText(user.getName());
        } else {
            user.setName(jTextFieldUserName.getText());
        }
    }//GEN-LAST:event_jTextFieldUserNameFocusLost

    private void jTextFieldEmailMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextFieldEmailMouseClicked
        setJTextFieldEditMode(jTextFieldEmail);
    }//GEN-LAST:event_jTextFieldEmailMouseClicked

    private void jTextFieldEmailFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldEmailFocusLost
        setJTextFieldNonEditMode(jTextFieldEmail);
        if (!verifyEmail(jTextFieldEmail.getText(), this)) {
            jTextFieldEmail.setText(user.getEmail());
        } else {
            user.setEmail(jTextFieldEmail.getText());
        }
    }//GEN-LAST:event_jTextFieldEmailFocusLost

    private void jTextFieldFirstNameMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextFieldFirstNameMouseClicked
        setJTextFieldEditMode(jTextFieldFirstName);
    }//GEN-LAST:event_jTextFieldFirstNameMouseClicked

    private void jTextFieldFirstNameFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldFirstNameFocusLost
        setJTextFieldNonEditMode(jTextFieldFirstName);
        user.setFirstName(jTextFieldFirstName.getText());
    }//GEN-LAST:event_jTextFieldFirstNameFocusLost

    private void jTextFieldLastNameMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextFieldLastNameMouseClicked
        setJTextFieldEditMode(jTextFieldLastName);
    }//GEN-LAST:event_jTextFieldLastNameMouseClicked

    private void jTextFieldLastNameFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldLastNameFocusLost
        setJTextFieldNonEditMode(jTextFieldLastName);
        user.setLastName(jTextFieldLastName.getText());
    }//GEN-LAST:event_jTextFieldLastNameFocusLost

    private void jComboBoxDayItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBoxDayItemStateChanged
        if (!comboDayInited) {
            return;
        }
        jPanelSaveOrCancel.setVisible(true);
    }//GEN-LAST:event_jComboBoxDayItemStateChanged

    private void jComboBoxMonthItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBoxMonthItemStateChanged
        if (!comboMonthInited) {
            return;
        }
        jPanelSaveOrCancel.setVisible(true);
    }//GEN-LAST:event_jComboBoxMonthItemStateChanged

    private void jComboBoxYearItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBoxYearItemStateChanged
        if (!comboYearInited) {
            return;
        }
        jPanelSaveOrCancel.setVisible(true);
    }//GEN-LAST:event_jComboBoxYearItemStateChanged

    private void jComboBoxCountryItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBoxCountryItemStateChanged
        if (!comboCountryInited) {
            return;
        }
        jPanelSaveOrCancel.setVisible(true);
    }//GEN-LAST:event_jComboBoxCountryItemStateChanged

    private void jComboBoxGenderItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBoxGenderItemStateChanged
        if (!comboGenderInited) {
            return;
        }
        jPanelSaveOrCancel.setVisible(true);
    }//GEN-LAST:event_jComboBoxGenderItemStateChanged

    private void jLabelSaveMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabelSaveMouseClicked
        updateUserInfo();
        jPanelSaveOrCancel.setVisible(false);
        jPanelMyLoans.requestFocus();
    }//GEN-LAST:event_jLabelSaveMouseClicked

    private void jLabelCancelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabelCancelMouseClicked
        initHeaderComponents();
        jPanelMyLoans.requestFocus();
    }//GEN-LAST:event_jLabelCancelMouseClicked

    private void formFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_formFocusGained
    }//GEN-LAST:event_formFocusGained

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
    }//GEN-LAST:event_formComponentShown

    private void jPanelBooksComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_jPanelBooksComponentShown
        // TODO add your handling code here:
    }//GEN-LAST:event_jPanelBooksComponentShown
    // Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JComboBox jComboBoxCountry;
    private javax.swing.JComboBox jComboBoxDay;
    private javax.swing.JComboBox jComboBoxGender;
    private javax.swing.JComboBox jComboBoxMonth;
    private javax.swing.JComboBox jComboBoxYear;
    private javax.swing.JLabel jLabelBirthday;
    private javax.swing.JLabel jLabelCancel;
    private javax.swing.JLabel jLabelCountry;
    private javax.swing.JLabel jLabelFirstName;
    private javax.swing.JLabel jLabelGender;
    private javax.swing.JLabel jLabelImage;
    private javax.swing.JLabel jLabelLastName;
    private javax.swing.JLabel jLabelSave;
    private javax.swing.JPanel jPanel;
    private javax.swing.JPanel jPanel12;
    private javax.swing.JPanel jPanelBirthday;
    private javax.swing.JPanel jPanelBooks;
    private javax.swing.JPanel jPanelCountry;
    private javax.swing.JPanel jPanelDate;
    private javax.swing.JPanel jPanelDesc;
    private javax.swing.JPanel jPanelFirstName;
    private javax.swing.JPanel jPanelGender;
    private javax.swing.JPanel jPanelLastName;
    private javax.swing.JPanel jPanelMyBooks;
    private javax.swing.JPanel jPanelMyLoans;
    private javax.swing.JPanel jPanelRating;
    private javax.swing.JPanel jPanelSaveOrCancel;
    private javax.swing.JPanel jPanelUserHeader;
    private javax.swing.JPanel jPanelUserInfoHeaderWithPic;
    private javax.swing.JPanel jPanelUserTitle;
    private javax.swing.JScrollPane jScrollPaneBooks;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextField jTextFieldEmail;
    private javax.swing.JTextField jTextFieldFirstName;
    private javax.swing.JTextField jTextFieldLastName;
    private javax.swing.JTextField jTextFieldUserName;
    // End of variables declaration//GEN-END:variables

    private String[] getIntList(int start, int end) {
        String[] arr = new String[end - start + 1];
        for (int i = 0; i <= end - start; i++) {
            arr[i] = "" + (start + i);
        }
        return arr;
    }

    private void initHeaderComponents() {
        jTextFieldUserName.setText(user.getName());
        final JPanelMyProfile thisObj = this;
        GUIUtils.setProfileImageAtLabel(user, jLabelImage, 100, 130);
        GUIUtils.setJComponentFontAndColor(jTextFieldUserName, 15);

        GUIUtils.initTextHeaderJlabel(jTextFieldEmail, user.getEmail(), true);

        GUIUtils.setJComponentFontAndColor(jLabelFirstName, 13);
        GUIUtils.initTextHeaderJlabel(jTextFieldFirstName, user.getFirstName(), true);

        GUIUtils.setJComponentFontAndColor(jLabelLastName, 13);
        GUIUtils.initTextHeaderJlabel(jTextFieldLastName, user.getLastName(), true);

        GUIUtils.setJComponentFontAndColor(jLabelBirthday, 13);
        GUIUtils.setJComponentFontAndColor(jLabelCountry, 13);
        GUIUtils.setJComponentFontAndColor(jLabelGender, 13);

        if (user.getCountryID() > 0) {
            DBAccessThread.invoke(new IDBAccessThread() {
                @Override
                public Object onCreateQuery() throws DBAccessException {
                    return DBAccessEngine.Country().select(user.getCountryID()).get(0);
                }

                @Override
                public void onAccessFinished(Object response) {
                    jComboBoxCountry.setSelectedItem(response);
                }

                @Override
                public void onAccessFailed(DBAccessException e) {
                    GUIUtils.showMessageError(e, thisObj);
                }
            });
        }
        jComboBoxGender.setSelectedItem(user.getGender());
        jComboBoxDay.setSelectedItem(user.getBirthday().get(Calendar.DAY_OF_MONTH) + "");
        jComboBoxMonth.setSelectedItem(user.getBirthday().get(Calendar.MONTH) + 1 + "");
        jComboBoxYear.setSelectedItem(user.getBirthday().get(Calendar.YEAR) + "");

        if (usersRater == null) {
            DBAccessThread.invoke(new IDBAccessThread() {
                @Override
                public Object onCreateQuery() throws DBAccessException {
                    int[] arr = new int[2];
                    arr[0] = DBAccessEngine.UserComment().selectCount(user.getID(), null);
                    arr[1] = Math.round(DBAccessEngine.UserComment().selectAvg(user.getID()));
                    return arr;
                }

                @Override
                public void onAccessFinished(Object response) {
                    int[] arr = (int[]) response;
                    usersRater = new Rater(arr[1], arr[0]);
                    usersRater.setEnabled(false);
                    jPanelRating.add(usersRater);
                }

                @Override
                public void onAccessFailed(DBAccessException e) {
                    GUIUtils.showMessageError(e, thisObj);
                }
            });


        }
        GUIUtils.setImageAtSize(jLabelSave, 30, 30, "/resources/confirm.png");
        GUIUtils.setURLLink(jLabelSave, null, false);
        GUIUtils.setImageAtSize(jLabelCancel, 30, 30, "/resources/cancel.png");
        GUIUtils.setURLLink(jLabelCancel, null, false);
        jPanelSaveOrCancel.setVisible(false);
    }

    private void setJTextFieldEditMode(JTextField jtf) {
        if (jtf.getBackground().equals(Color.WHITE)) {
            return;
        }
        jtf.setBackground(Color.WHITE);
        jtf.setForeground(Color.BLACK);
        jtf.setFocusable(true);
        jtf.setEditable(true);
        jtf.requestFocus();
        jtf.setSelectionStart(0);
        jtf.setSelectionEnd(jtf.getText().length());
        jPanelSaveOrCancel.setVisible(true);
    }

    private void setJTextFieldNonEditMode(JTextField jtf) {
        jtf.setBackground(new Color(51, 153, 255));
        jtf.setForeground(new java.awt.Color(153, 255, 153));
        jtf.setFocusable(false);
        jtf.setEditable(false);
    }

    private void initBooksPanel() {
        final JPanelMyProfile thisObj = this;
        DBAccessThread.invoke(new IDBAccessThread() {
            @Override
            public Object onCreateQuery() throws DBAccessException {
                return DBAccessEngine.User().selectAll(user.getID(), new Book(0, ""));
            }

            @SuppressWarnings("unchecked")
            @Override
            public void onAccessFinished(Object response) {
                jPanelBooks.removeAll();
                for (Book book : (List<Book>) response) {
                    jPanelBooks.add(new JPanelBookInfoForUsersBookSearch(book, user, false));
                }
            }

            @Override
            public void onAccessFailed(DBAccessException e) {
                GUIUtils.showMessageError(e, thisObj);
            }
        });

    }

    @SuppressWarnings("unchecked")
    private void updateUserInfo() {
        try {
            User user = MainForm.getMyUser();
            user.setCityID(1);
            user.setName(jTextFieldUserName.getText());
            if (jComboBoxCountry.getSelectedIndex() == 0) {
                user.setCountryID(-1);
            } else {
                user.setCountryID(jComboBoxCountry.getSelectedIndex());
            }
            user.setEmail(jTextFieldEmail.getText());
            user.setFirstName(jTextFieldFirstName.getText());
            user.setLastName(jTextFieldLastName.getText());
            Calendar cal = Calendar.getInstance();
            int year = Integer.parseInt(jComboBoxYear.getSelectedItem().toString());
            int month = Integer.parseInt(jComboBoxMonth.getSelectedItem().toString());
            int day = Integer.parseInt(jComboBoxDay.getSelectedItem().toString());
            cal.set(year, month - 1, day, 0, 0, 0);
            user.setBirthday(cal);
            ((IDBAccess<User>) DBAccessEngine.User()).update(user);
        } catch (DBAccessException ex) {
            GUIUtils.showMessageError(ex, this);
        }
    }

    private void initLoansPanel() {
        final List<BookLoan> loansFromMe = new ArrayList<BookLoan>();
        final List<BookLoan> loansToMe = new ArrayList<BookLoan>();
        final JPanelMyProfile thisObj = this;

        DBAccessThread.invoke(new IDBAccessThread() {
            @Override
            public Object onCreateQuery() throws DBAccessException {
                return DBAccessEngine.BookLoan().select(MainForm.getMyUser().getID());
            }

            @SuppressWarnings("unchecked")
            @Override
            public void onAccessFinished(Object response) {
                for (BookLoan bl : (List<BookLoan>) response) {
                    if (bl.getLoanedTo() == MainForm.getMyUser().getID()) {
                        loansToMe.add(bl);
                    } else {
                        loansFromMe.add(bl);
                    }
                }
                jPanelMyLoans.add(new JPanelLoansList(true, loansFromMe));
                jPanelMyLoans.add(new JPanelLoansList(false, loansToMe));
            }

            @Override
            public void onAccessFailed(DBAccessException e) {
                GUIUtils.showMessageError(e, thisObj);
            }
        });
        try {
            Thread.sleep(400);
            comboDayInited = true;
            comboMonthInited = true;
            comboYearInited = true;
            comboGenderInited = true;
            comboCountryInited = true;
        } catch (InterruptedException ex) {
            Logger.getLogger(JPanelMyProfile.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
